﻿Array.prototype.contains = function (item) {
    var i = this.length
    while (i-- > 0) {
        if (this[i] === item) {
            return true;
        }
    }
    return false;
};

Array.prototype.remove = function (item) {
    for (var i = 0, len = this.length; i < len; i++) {
        if (this[i] === item) {
            i === (len - 1) ? this.pop() : this[i] = this.pop();
            return;
        }
    }
};

Array.prototype.removeWithComparer = function (item, comparer) {
    for (var i = 0, len = this.length; i < len; i++) {
        if (comparer(this[i], item)) {
            i === (len - 1) ? this.pop() : this[i] = this.pop();
            return;
        }
    }
};

Array.prototype.removeRange = function (items) {
    for (var i = 0; i < items.length; i++) {
        this.remove(items[i]);
    }
};

Array.prototype.clear = function (item) {
    while (this.length > 0) {
        this.pop();
    }
    return this;
};

Array.prototype.pushArray = function (arr) {
    for (var i = 0, len = arr.length; i < len; i++) {
        this.push(arr[i]);
    }
};

Array.prototype.insertOrdered = function (value) {
    var poped = []
    var len = this.length;
    var i = len;
    while (i--) {
        var pop = this[i];

        if (pop <= value) {
            this.push(value);
            break;
        }
        else {
            poped.push(this.pop());
        }
    }

    if (i === -1)
        this.push(value);

    var popIndex = len - (i + 1);
    while (popIndex--) {
        this.push(poped[popIndex]);
    }
};

Array.prototype.removeOrdered = function (value) {
    var poped = []
    var len = this.length;
    var i = len;
    while (i--) {
        var pop = this.pop();
        if (pop === value) {
            len--;
            break;
        } else {
            poped.push(pop);
        }
    }

    var popIndex = i === -1 ? len : len - i;
    while (popIndex--) {
        this.push(poped[popIndex]);
    }

    return i !== -1;
}

Array.prototype.removeRangeOrdered = function (items) {
    for (var i = 0; i < items.length; i++) {
        this.removeOrdered(items[i]);
    }
};

Array.prototype.equalsSorted = function (otherSorted) {
    var tmp = this.length;
    if (tmp !== otherSorted.length) return false;

    while (tmp--) if (this[tmp] !== otherSorted[tmp]) return false;

    return true;
};

Array.prototype.hasSubArray = function (sortedSubArray) {
    var otherLength = sortedSubArray.length;
    if (otherLength > this.length) return false;

    var otherIndex = otherLength;
    var poped = [];
    while (otherIndex--) {
        if (!this.contains(sortedSubArray[otherIndex]))
            return false;
    }

    return true;
};

Array.prototype.distinct = function () {
    var set = [];
    for (var i = 0, len = this.length; i < len; i++) {
        var value = this[i];
        for (var j = set.length; j--;) {
            if (value === set[j]) {
                break;
            }
        }
        if (j < 0) {
            set.push(value);
        }
    }
    return set;
}

Array.prototype.selectSort = function () {
    var min, tmp;
    var len = this.length;
    for (var i = 0; i < len; i++) {
        min = i;
        for (var j = i + 1; j < len; j++) {
            if (this[min] > this[j]) {
                min = j;
            }
        }
        if (min != i) {
            tmp = this[i];
            this[i] = this[min];
            this[min] = tmp;
        }
    }

    return this;
}

Array.prototype.shuffle = function () {
    for (var i = this.length - 1; i > 0; i--) {
        var j = Math.floor(Math.random() * (i + 1));
        var temp = this[i];
        this[i] = this[j];
        this[j] = temp;
    }
    return this;
}